package com.sias.oj.model.vo;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sias.oj.model.entity.Post;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 帖子视图
 *
 * @author <a href="https://github.com/sogra">程序员sogra</a>
 */
@Data
public class PostVO implements Serializable {

  private final static Gson GSON = new Gson();

  /**
   * id
   */
  private Long id;

  /**
   * 标题
   */
  private String title;

  /**
   * 内容
   */
  private String content;

  /**
   * 点赞数
   */
  private Integer thumbNum;

  /**
   * 收藏数
   */
  private Integer favourNum;

  /**
   * 创建用户 id
   */
  private Long userId;

  /**
   * 创建时间
   */
  private Date createTime;

  /**
   * 更新时间
   */
  private Date updateTime;

  /**
   * 标签列表
   */
  private List<String> tagList;

  /**
   * 创建人信息
   */
  private UserVO user;

  /**
   * 是否已点赞
   */
  private Boolean hasThumb;

  /**
   * 是否已收藏
   */
  private Boolean hasFavour;

  /**
   * 包装类转对象
   *
   * @param postVO
   * @return
   */
  public static Post voToObj(PostVO postVO) {
    if (postVO == null) {
      return null;
    }
    Post post = new Post();
    BeanUtils.copyProperties(postVO, post);
    List<String> tagList = postVO.getTagList();
    if (tagList != null) {
      post.setTags(GSON.toJson(tagList));
    }
    return post;
  }

  /**
   * 对象转包装类
   *
   * @param post
   * @return
   */
  public static PostVO objToVo(Post post) {
    if (post == null) {
      return null;
    }
    PostVO postVO = new PostVO();
    BeanUtils.copyProperties(post, postVO);
    postVO.setTagList(GSON.fromJson(post.getTags(), new TypeToken<List<String>>() {
    }.getType()));
    return postVO;
  }
}
